﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using System.Data.Common;

namespace SP.OpenSource.CDC.Monitor
{
    public partial class frmConnect : Form
    {
        DataSet m_dsConfigurations = new DataSet();
        bool m_bNew = false;
        bool m_bEdit = false;

        String m_sCDCConnection = String.Empty;
        int m_iSelectedConfiguration = -1;
        String m_sExePath = String.Empty;
        String m_sFormTitle = "Connect to CDC Database";
        String m_sEncryTypeWarning = String.Empty;

        BuildConnectionString m_oBuildConnectionString = new BuildConnectionString(String.Empty, String.Empty);

        public frmConnect()
        {
            InitializeComponent();
        }

        
        
        private void frmConnect_Load(object sender, EventArgs e)
        {
            m_sExePath = System.Windows.Forms.Application.StartupPath.ToString();
            m_sEncryTypeWarning = "Make sure to re-enter and save all passwords, as applicable."+Environment.NewLine + "(CDC and/or Database connection,SMTP authentication etc.)";
            ReloadUI();
        }

        #region  Helpers
        private void ReloadUI()
        {
            if (false == System.IO.File.Exists(m_sExePath + "\\Config\\Configuration.dat"))
            {
                MessageBox.Show("Login error. Looks like product installation was not complete."  , "Login error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                this.Close();
                return;
            }

            //Clean data table to avoid duplication.
            if (null != m_dsConfigurations && m_dsConfigurations.Tables.Count > 0 && m_dsConfigurations.Tables[0].Rows.Count > 0)
                m_dsConfigurations.Tables.RemoveAt(0);

            m_dsConfigurations.ReadXml(m_sExePath + "\\Config\\Configuration.dat", XmlReadMode.ReadSchema);
            if (null == m_dsConfigurations || m_dsConfigurations.Tables[0].Rows.Count <= 1)
            {
                m_bNew = true;
                
                grpConfiguration.Visible = false;
                tsbtnSave.Visible = false;
                tsSepSave.Visible = false;
                tspCancel.Visible = false;
                tsSepCancel.Visible = false;
                tspDelete.Visible = false;
                tsSepDelete.Visible = false;
                tsSepDelete.Visible = false;
                tsbtnEdit.Visible = false;
                tsSepEdit.Visible = false;

                grpDBConnection.Visible = false;
                tsbtnNewSys.Visible = true;
                m_iSelectedConfiguration = -1;
            }
            else
            {
                m_bNew = false;
                tsbtnNewSys.Visible = true;
                
                grpConfiguration.Visible = true;
                tsbtnSave.Visible = false;
                tsSepSave.Visible = false;
                tspCancel.Visible = false;
                tsSepCancel.Visible = false;
                tspDelete.Visible = true;
                tsSepDelete.Visible = true;
                
                tsbtnEdit.Visible = true;
                tsSepEdit.Visible = true;
                grpDBConnection.Visible = false;
                grpDBConnection.Enabled = false;
                btnConnect.Enabled = false;
                String sErrorMessage = String.Empty;
                if (false == CleanConfigDropdown(ref sErrorMessage))
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(sErrorMessage, m_sFormTitle + " - Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
                    
                    tspCancel.Visible = false;
                    tsSepCancel.Visible = false;
                    return;
                }
                cmbConfigurations.DataSource = m_dsConfigurations.Tables[0];
                cmbConfigurations.DisplayMember = "ConfigurationName";
                cmbConfigurations.ValueMember = "ConfigurationID";

                cmbConfigurations.SelectedValue = -1;
                m_iSelectedConfiguration = -1;

                tspSepCopyConfig.Visible = false;
                if (cmbConfigurations.Text == "Select a connection")
                {
                    tspDelete.Visible = false;
                    tsSepDelete.Visible = false;

                    tsbtnEdit.Visible = false;
                    tsSepEdit.Visible = false;
                    
                }

            }
        }
        private bool CleanConfigDropdown(ref String sErrorMessage)
        {
            try
            {
                DataTable dtTmp = new DataTable();
                cmbConfigurations.DataSource = dtTmp;
                sErrorMessage = String.Empty;
                return true;
            }
            catch (Exception ex)
            {
                sErrorMessage = ex.Message;
                return false;
            }
        }
        private void SetCancelGUI()
        {
            m_bNew = false;
            m_bEdit = false;
            
            tsbtnNewSys.Visible = true;
         
            grpConfiguration.Visible = true;
            grpConfiguration.Enabled = true;
            tsbtnSave.Visible = false;
            tsSepSave.Visible = false;
            tspCancel.Visible = false;
            tsSepCancel.Visible = false;
            tspDelete.Visible = false;
            tsSepDelete.Visible = false;
            tsbtnEdit.Visible = false;
            tsSepEdit.Visible = false;
         
            tspSepCopyConfig.Visible = false;
            grpDBConnection.Visible = false;
            grpDBConnection.Enabled = false;

            lblselectConfiguration.Visible = true;
            
            cmbConfigurations.SelectedValue = -1;
            btnConnect.Enabled = false;

            lblselectConfiguration.Text = "Connection:";
        }
        #endregion //Helpers

        private void tsbtnNewSys_Click_1(object sender, EventArgs e)
        {

            m_bNew = true;
            m_bEdit = false;

            tsbtnNewSys.Visible = false;
            grpConfiguration.Visible = false;
            tsbtnSave.Visible = true;
            tsSepSave.Visible = true;
            tspCancel.Visible = true;
            tsSepCancel.Visible = true;
            tspDelete.Visible = false;
            tsSepDelete.Visible = false;
            tsbtnEdit.Visible = false;
            tsSepEdit.Visible = false;
            grpDBConnection.Visible = true;
            grpDBConnection.Enabled = true;
            txtConfigName.Text = String.Empty;


            txtCDCDB.Text = String.Empty;
            chkIntegratedSecurity.Checked = true;
            txtDBServer.Text = String.Empty;
            txtUsername.Text = String.Empty;
            txtPassword.Text = String.Empty;

            m_iSelectedConfiguration = -1;
            tspSepCopyConfig.Visible = false;

        }

        private void tsbtnEdit_Click(object sender, EventArgs e)
        {
            if (cmbConfigurations.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show("Please select a connection", "Manage connections", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            m_bNew = false;
            m_bEdit = true;
            tsbtnNewSys.Visible = false;
            grpConfiguration.Visible = true;
            grpConfiguration.Enabled = false;
            tsbtnSave.Visible = true;
            tsSepSave.Visible = true;
            tspCancel.Visible = true;
            tsSepCancel.Visible = true;
            tspDelete.Visible = false;
            tsSepDelete.Visible = false;
            tsbtnEdit.Visible = false;
            tsSepEdit.Visible = false;
            grpDBConnection.Visible = true;
            grpDBConnection.Enabled = true;
            tspSepCopyConfig.Visible = false;

        }

        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                String sErrorMessage = String.Empty;

                if (sErrorMessage.Length > 0)
                {
                    MessageBox.Show("There was a problem while saving to connection information." + Environment.NewLine + "Error Message : " + sErrorMessage, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (txtConfigName.Text.Length <= 0 || txtCDCDB.Text.Length <= 0 || txtDBServer.Text.Length <= 0)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Configuration or Connection strings cannot be empty.", m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                if (chkIntegratedSecurity.Checked == false &&
                    (txtUsername.Text.Length <= 0 || txtPassword.Text.Length <= 0))
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Username/Password cannot be empty.", m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                String sConn = String.Empty;
                String sEncryptedPassword = String.Empty;
                String sEncryptionType = (rdbUserKey.Checked == true? "USERKEY" :"MACHINEKEY");

                if (chkIntegratedSecurity.Checked == true)
                {
                    sConn = BLL.Utils.BuildConnectionString(txtDBServer.Text, txtCDCDB.Text,
                                                                      chkIntegratedSecurity.Checked, String.Empty, String.Empty,
                                                                      String.Empty, ref sErrorMessage);
                }
                else
                {
                    sConn = BLL.Utils.BuildConnectionString(txtDBServer.Text, txtCDCDB.Text,
                                                                   chkIntegratedSecurity.Checked, txtUsername.Text, String.Empty,
                                                                   String.Empty, ref sErrorMessage);
                    sErrorMessage = String.Empty;
                    if (false == BLL.Crypt.Encrypt(txtPassword.Text, sEncryptionType, ref sEncryptedPassword, ref sErrorMessage))
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("There was an issue while encrypting password." + Environment.NewLine + "ErrorMessage: " + sErrorMessage, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

                if (m_bNew == true)
                {
                    //Check connection String & Confi name

                    if (null == m_oBuildConnectionString)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("There was an error processing request.", m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    DbProviderFactory factory =
                            DbProviderFactories.GetFactory("System.Data.SqlClient");

                    DataRow drT = m_dsConfigurations.Tables[0].NewRow();
                    drT["ConfigurationName"] = txtConfigName.Text;
                    drT["ConnectionString"] = sConn;
                    drT["PasswordEncryptionType"] = sEncryptionType;

                    if(chkIntegratedSecurity.Checked == false)
                        drT["EncryptedConnectionPassword"] = sEncryptedPassword;

                    m_dsConfigurations.Tables[0].Rows.Add(drT);
                    m_dsConfigurations.AcceptChanges();
                    m_bNew = false;

                }
                else if (m_bEdit == true)
                {
                    DataRow[] drDups = m_dsConfigurations.Tables[0].Select("ConfigurationID=" + cmbConfigurations.SelectedValue.ToString());
                    if (drDups.Length <= 0)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Connection not found!.", m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    foreach (DataRow drT in m_dsConfigurations.Tables[0].Rows)
                    {
                        if (drT["ConfigurationID"].ToString() == m_iSelectedConfiguration.ToString())
                        {
                            drT["ConfigurationName"] = txtConfigName.Text;
                            drT["ConnectionString"] = sConn;

                            drT["PasswordEncryptionType"] = sEncryptionType;

                            if (chkIntegratedSecurity.Checked == false)
                                drT["EncryptedConnectionPassword"] = sEncryptedPassword;                        

                        }
                    }
                    m_dsConfigurations.AcceptChanges();
                    m_bEdit = false;
                    grpConfiguration.Enabled = true;
                    lblselectConfiguration.Text = "Connection:";
                }

                m_dsConfigurations.WriteXml(m_sExePath + "\\Config\\Configuration.dat", XmlWriteMode.WriteSchema);

                cmbConfigurations.DataSource = m_dsConfigurations.Tables[0];
                cmbConfigurations.DisplayMember = "ConfigurationName";
                cmbConfigurations.ValueMember = "ConfigurationID";

                cmbConfigurations.SelectedValue = -1;
                m_iSelectedConfiguration = -1;
                tsbtnNewSys.Visible = true;

                
                grpConfiguration.Visible = true;
                tsbtnSave.Visible = false;
                tsSepSave.Visible = false;
                tspCancel.Visible = false;
                tsSepCancel.Visible = false;
                tspDelete.Visible = true;
                tsSepDelete.Visible = true;
                tsbtnEdit.Visible = true;
                tsSepEdit.Visible = true;
                grpDBConnection.Visible = false;
                grpDBConnection.Enabled = false;

                
                
                SetCancelGUI();

                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                SetCancelGUI();
                MessageBox.Show("There was an error processing request. " + ex.Message, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
            }
        }

        private void tspCancel_Click(object sender, EventArgs e)
        {
            SetCancelGUI();         
        }

        private void tspDelete_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                if (cmbConfigurations.SelectedValue.ToString() == "-1")
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Please select a connection.", "Delete connection?", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                String sMessage = "Note: Deleting connection will NOT change or delete anything in the database.";
                sMessage += Environment.NewLine;
                sMessage += "Are you sure you want to delete the connection " + cmbConfigurations.Text.ToString() + "?";

                if (DialogResult.Yes == MessageBox.Show(sMessage, "Delete connection?", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                {
                    //Truncate all meta tables for this confguration
                    String sErrMsg = String.Empty;
                    
                    String sErrorMessage = String.Empty;
                    
                    //Delete config from file
                    DataRow[] drDups = m_dsConfigurations.Tables[0].Select("ConfigurationName='" + txtConfigName.Text + "'");
                    if (drDups.Length <= 0)
                    {
                        MessageBox.Show("Connection not found!.", m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        Cursor.Current = Cursors.Default;
                        return;
                    }


                    m_dsConfigurations.Tables[0].Rows.Remove(drDups[0]);
                    m_dsConfigurations.AcceptChanges();
                    m_dsConfigurations.WriteXml(m_sExePath + "\\Config\\Configuration.dat", XmlWriteMode.WriteSchema);

                    txtDBServer.Text = String.Empty;
                    txtCDCDB.Text = String.Empty;
                    txtUsername.Text = String.Empty;
                    txtPassword.Text = String.Empty;
                    txtConfigName.Text = String.Empty;
                    chkIntegratedSecurity.Checked = true;

                    if (null == m_dsConfigurations || m_dsConfigurations.Tables[0].Rows.Count <= 1)
                    {
                        m_bNew = true;

                        tsbtnNewSys.Visible = false;
                        grpConfiguration.Visible = false;
                        tsbtnSave.Visible = true;
                        tsSepSave.Visible = true;
                        tspCancel.Visible = false;
                        tsSepCancel.Visible = false;
                        tspDelete.Visible = false;
                        tsSepDelete.Visible = false;
                        tsbtnEdit.Visible = false;
                        tsSepEdit.Visible = false;
                        grpDBConnection.Visible = true;
                        grpDBConnection.Enabled = true;
                        m_iSelectedConfiguration = -1;
                    }
                    else
                    {
                        m_bNew = false;
                        tsbtnNewSys.Visible = true;
                    }

                    ReloadUI();
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Connection deleted.", m_sFormTitle, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error while processing request." + Environment.NewLine + Environment.NewLine + "Error Message: " + ex.Message, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Cursor.Current = Cursors.Default;
            }
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                String sErrorMessage = String.Empty;
                

                String sConn = BLL.Utils.BuildConnectionString(txtDBServer.Text, txtCDCDB.Text,
                                                               chkIntegratedSecurity.Checked, txtUsername.Text, txtPassword.Text,
                                                               String.Empty, ref sErrorMessage);

                if (sErrorMessage.Length > 0)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("There was a problem while connecting to database." + Environment.NewLine + "Error Message : " + sErrorMessage, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                String sEncryType = (rdbUserKey.Checked == true ? "USERKEY": "MACHINEKEY");
                MonitorCDCMain objCDCControllerMain = new MonitorCDCMain(sConn,  cmbConfigurations.Text, cmbConfigurations.SelectedValue.ToString(), sEncryType);
                this.Hide();
                
                objCDCControllerMain.ShowDialog();

                if (objCDCControllerMain.m_bLogout == true)
                {
                    this.cmbConfigurations.SelectedValue = -1;
                    btnConnect.Enabled = false;
                    grpDBConnection.Visible = false;
                    tsbtnEdit.Visible = false;
                    tsbtnSave.Visible = false;
                    tspCancel.Visible = false;
                    tspDelete.Visible = false;

                    tsSepCancel.Visible = false;
                    tsSepDelete.Visible = false;

                    this.Show();
                }
                else
                    this.Close();

                Cursor.Current = Cursors.Default;
            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                String sMsg = "There was an error processing." + Environment.NewLine + "Error Message: ";

                MessageBox.Show(sMsg + ex.Message, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
               
                this.Close();
            }
        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {   
                Cursor.Current = Cursors.WaitCursor;
                if (txtDBServer.Text.Length <= 0 || txtCDCDB.Text.Length <= 0)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Server / Database name cannot be blank.", m_sFormTitle + " - Test Connection ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (false == chkIntegratedSecurity.Checked)
                {
                    if (txtUsername.Text.Length <= 0 || txtPassword.Text.Length <= 0)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Database username/password cannot be blank.", m_sFormTitle + " - Test Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;

                    }
                }
                String sErrorMessage = String.Empty;
                String sConn = BLL.Utils.BuildConnectionString(txtDBServer.Text, txtCDCDB.Text,
                                                         chkIntegratedSecurity.Checked, txtUsername.Text, txtPassword.Text,
                                                         String.Empty, ref sErrorMessage);
                if (sErrorMessage.Length > 0)
                {
                    String sError = "There was an error while connecting to database.";
                    sError += Environment.NewLine + Environment.NewLine;
                    sError += sErrorMessage;

                    MessageBox.Show(sError, m_sFormTitle + " - Test Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    Cursor.Current = Cursors.Default;
                    return;
                }

                SqlConnection sqlMetaConn = new SqlConnection(sConn);
                sqlMetaConn.Open();

                Cursor.Current = Cursors.Default;
                MessageBox.Show("Test connection completed successfully", m_sFormTitle + " - Test Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
            catch (Exception ex)
            {
                Cursor.Current = Cursors.Default;
                String sError = "There was an error while connecting to database.";
                sError += Environment.NewLine + Environment.NewLine;
                sError += ex.Message;

                MessageBox.Show(sError, m_sFormTitle + " - Test Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        
        private void cmbConfigurations_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                if (cmbConfigurations.SelectedValue.ToString() == "-1" ||
                    cmbConfigurations.SelectedValue.ToString() == "System.Data.DataRowView" ||
                    cmbConfigurations.Text == "Select a connection")
                {
                    grpDBConnection.Visible = false;
                    btnConnect.Enabled = false;
                    tsbtnEdit.Visible = false;
                    tsSepEdit.Visible = false;
                    tsbtnSave.Visible = false;
                    tsSepSave.Visible = false;
                    tspCancel.Visible = false;
                    tsSepCancel.Visible = false;
                    tspDelete.Visible = false;
                    tsSepDelete.Visible = false;
                    m_iSelectedConfiguration = -1;

                    return;
                }
                else
                {

                    if (m_bEdit == false)
                    {

                        tspSepCopyConfig.Visible = true;

                        m_iSelectedConfiguration = Convert.ToInt32(cmbConfigurations.SelectedValue.ToString());
                        grpDBConnection.Visible = true;
                        btnConnect.Visible = true;
                        btnConnect.Enabled = true;

                        tsbtnEdit.Visible = true;
                        tsSepEdit.Visible = true;
                        tsbtnSave.Visible = false;
                        tsSepSave.Visible = false;
                        tspCancel.Visible = false;
                        tsSepCancel.Visible = false;
                        tspDelete.Visible = true;
                        tsSepDelete.Visible = true;
                        tsSepDelete.Visible = true;

                        m_sCDCConnection = cmbConfigurations.SelectedValue.ToString();


                        DataRow[] drSelected = m_dsConfigurations.Tables[0].Select("ConfigurationID=" + cmbConfigurations.SelectedValue.ToString());

                        if (drSelected.Length > 0)
                        {
                            txtConfigName.Text = drSelected[0]["ConfigurationName"].ToString();

                            String sServer = String.Empty;
                            String sDBName = String.Empty;

                            String sIntegrartedSecurity = String.Empty;
                            String sUser = String.Empty;
                            String sPass = String.Empty;
                            String sProvider = String.Empty;
                            String sErrorMessage = String.Empty;
                            if (false == BLL.Utils.ParseConnectionString(drSelected[0]["ConnectionString"].ToString(),
                                                                            ref sServer, ref sDBName, ref  sProvider, ref sIntegrartedSecurity,
                                                                            ref sUser, ref sPass, ref  sErrorMessage))
                            {
                                MessageBox.Show("There was an error while processing your request." + Environment.NewLine + "Error Message: " + sErrorMessage,
                                               m_sFormTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            txtDBServer.Text = sServer;
                            txtCDCDB.Text = sDBName;
                            if (sIntegrartedSecurity.ToUpper() == "TRUE")
                            {
                                chkIntegratedSecurity.Checked = true;
                                txtUsername.Text = String.Empty;
                                txtUsername.Visible = false;

                                txtPassword.Text = String.Empty;
                                txtPassword.Visible = false;

                            }
                            else
                            {
                                chkIntegratedSecurity.Checked = false;
                                txtUsername.Text = sUser;
                                txtUsername.Visible = true;

                                String sDecryPass = String.Empty;
                                BLL.Crypt.Decrypt(drSelected[0]["EncryptedConnectionPassword"].ToString(), 
                                                drSelected[0]["PasswordEncryptionType"].ToString(), ref sDecryPass, ref sErrorMessage);
                                txtPassword.Text = sDecryPass;
                                txtPassword.Visible = true;

                            }


                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("There was an error while processing your request." + Environment.NewLine + "Error Message: " + ex.Message,
                                m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void chkIntegratedSecurity_CheckedChanged(object sender, EventArgs e)
        {
            if (chkIntegratedSecurity.Checked == true)
            {
                lblPassword.Visible = false;
                lblUsername.Visible = false;
                txtPassword.Visible = false;
                txtUsername.Visible = false;
            }
            else
            {
                lblPassword.Visible = true;
                lblUsername.Visible = true;
                txtPassword.Visible = true;
                txtUsername.Visible = true;
            }
        }

        private void rdbUserKey_CheckedChanged(object sender, EventArgs e)
        {
            if(rdbUserKey.Checked == true)
                MessageBox.Show(m_sEncryTypeWarning,m_sFormTitle, MessageBoxButtons.OK,MessageBoxIcon.Warning);
        }

        private void rdbMachineKey_CheckedChanged(object sender, EventArgs e)
        {
            if(rdbMachineKey.Checked == true)
                MessageBox.Show(m_sEncryTypeWarning, m_sFormTitle, MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            try
            {
                String sExeFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

                System.Diagnostics.Process.Start(sExeFilePath + "\\Help\\HelpMonitorCDC.chm");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception raised, while processing request. Message: " + ex.Message, m_sFormTitle + " - Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
